import express from 'express'
import bodyParser from 'body-parser'

import conf from './conf.js'
import routes from './routes/index.js'

const app = express()
const { port } = conf

// 连接数据库
import './db/mongo.js' // mongoDB
// import connect from './db/mysql.js' // mysqlDB
import './websocket/index.js'

// 使用body-parser中间件解析JSON格式的请求体
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

// 发布接口
const apis = await routes() // 等待接口导入
for (const name in apis) {
  if (Object.hasOwnProperty.call(apis, name)) {
    const prefix = name.split('.')[0]
    app.use(`/${prefix}`, apis[name]) // 注册接口
  }
}

// node server启动
app.listen(port, () => console.log('server is start,port is', port))

export default app
